package com.IR.user.action;

import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.IR.shop.po.Music;
import com.IR.user.service.AMusicBiz;
import com.IR.oss.util.OSSClientUtil2;

@Controller
public class AMusicAction {
	@Resource
	public AMusicBiz biz;
	
	@RequestMapping(value="saveMusic.do")
	@ResponseBody
	public void saveMusic(@RequestParam("file") MultipartFile[] files,Music music,HttpServletRequest request,HttpServletResponse response) throws Exception {
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setCharacterEncoding("utf-8");
		for(MultipartFile file:files) {
			OSSClientUtil2 ossClient = new OSSClientUtil2();
			if(file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1).equals("mp3")) {
				String name = ossClient.uploadImg2Oss(file);
				music.setMusicname(name);
			}else {
				String name1 = ossClient.uploadImg2Oss(file);
				music.setMusicphoto(name1);
			}	
			music.setMusicdate(new Date());
		}
		biz.saveMusic(music);
	}
	@RequestMapping(value="saveMusic2.do")
	@ResponseBody
	public void saveMusic2(@RequestParam("file") MultipartFile files,@RequestParam("files") MultipartFile filess,Music music,HttpServletRequest request,HttpServletResponse response) throws Exception {
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setCharacterEncoding("utf-8");	
		OSSClientUtil2 ossClient = new OSSClientUtil2();
		System.out.println("调用oss");
		String name = ossClient.uploadImg2Oss(filess);
		music.setMusicname(name);
		String name1 = ossClient.uploadImg2Oss(files);
		music.setMusicphoto(name1);	
		music.setMusicdate(new Date());
		biz.saveMusic(music);
		System.out.println("保存成功");
	}
}
